﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;
using DAO;
using BUS;
namespace QL_xxx
{
    public class KhachSan
    {

        public static int tinhTienThuePhong(PhieuThuePhongDTO PhieuThuePhong)
        {
            //so luong khach 
            int soKhach = PhieuThuePhong.SoLuongKhach;
            //tinh cac khoang tien thue phong
            int tienSuDungSanPham = tinhTienSuDungSanPham(PhieuThuePhong);
            int tienSuDungPhong = tienPhong(PhieuThuePhong);
            int tongTien = tienSuDungSanPham + tienSuDungPhong;

            //kiem tra khach trong nguoc hay khach nuoc ngoai
            List<KhachHangDTO> KhachHang = KhachHangBUS.LayDSKhachHang(PhieuThuePhong.MaKhachHang, null, null);
            bool laKhachTrongNuoc = KhachHang[0].LaKhachTrongNuoc;
            if (laKhachTrongNuoc == true)
            {
                if (soKhach == 3)
                {
                    List<QuyDinhDTO> DSQD = QuyDinhBUS.LayDSQuyDinh(-1, "Phụ thu khách trong nước");
                    tongTien = tongTien + tongTien * DSQD[0].MoTa/100;
                }  
            }
            else if (laKhachTrongNuoc == false)
            {
                List<QuyDinhDTO> DSQD = QuyDinhBUS.LayDSQuyDinh(-1, "Phu thu khách nước ngoài");
                tongTien = (int)(tongTien * DSQD[0].MoTa / 100);
            }

            return tongTien;
        }
        public static int TinhSoNgayThuePhong(PhieuThuePhongDTO PhieuThuePhong)
        {
            DateTime ngayBatDau = PhieuThuePhong.NgayThue;
            DateTime ngayTraPhong = PhieuThuePhong.NgayTra;
            int SoNgayThue = 0;
            SoNgayThue = ngayTuyetDoi(ngayTraPhong.Day, ngayTraPhong.Month, ngayTraPhong.Year) -
                                  ngayTuyetDoi(ngayBatDau.Day, ngayBatDau.Month, ngayBatDau.Year);
            if (ngayTraPhong.Year - ngayBatDau.Year == 1)
            {
                SoNgayThue = 365 + SoNgayThue;
                if (laNhamNhuan(ngayBatDau.Year) == true)
                {
                    SoNgayThue = SoNgayThue + 1;
                }
            }
            if (SoNgayThue == 0)
                SoNgayThue = 1;
            return SoNgayThue;
        }
        //tinh tien su dung san pham
        public static int tinhTienSuDungSanPham(PhieuThuePhongDTO PhieuThuePhong)
        {
            int soTien = 0;
            List<ChiTietPhong_SanPhamDTO> chitietThue = ChiTietPhong_SanPhamBUS.LayDSChiTietPhong_SanPham(-1, PhieuThuePhong.MaPhieuThuePhong);
            if (chitietThue.Count > 0 && chitietThue != null)
            {
                foreach (ChiTietPhong_SanPhamDTO chiTiet in chitietThue)
                {
                    List<SanPhamDTO> SanPham = SanPhamBUS.LayDSSanPham(chiTiet.MaSanPham, null);
                    soTien = soTien + SanPham[0].GiaSanPham * chiTiet.SoLuong;
                }
            }
            return soTien;
        }
        //tinh tien su dung phong
        public static int tienPhong(PhieuThuePhongDTO PhieuThuePhong)
        {
            int SoNgayThue = TinhSoNgayThuePhong(PhieuThuePhong);
            //Lay don gia phong
            PhongDTO Phong = PhongBUS.LayDSPhong(PhieuThuePhong.MaPhong, null, -1, -1)[0];
            LoaiPhongDTO LoaiPhong = LoaiPhongBUS.LayDSLoaiPhong(Phong.MaLoaiPhong, null)[0];
            return SoNgayThue * LoaiPhong.DonGia;
        }
        //Tinh so ngay tuyet doi de tinh so ngay thue phong
        public static int ngayTuyetDoi(int ngay, int thang, int nam)
        {
            int soNgay = 0;
            for (int i = 1; i < thang; i++)
            {
                soNgay = soNgay + soNgayTrongThang(i, nam);
            }
            soNgay = soNgay + ngay;
            return soNgay;
        }

        public static bool laNhamNhuan(int nam)
        {
            if (nam % 400 == 0 || (nam % 4 == 0 && nam % 100 != 0))
                return true;
            return false;
        }
        public static int soNgayTrongThang(int thang, int Nam)
        {
            int soNgay = 0; ;
            switch (thang)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    soNgay = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    soNgay = 30;
                    break;
                case 2:

                    soNgay = 28;
                    if (laNhamNhuan(Nam) == true)
                        soNgay = 29;
                    break;
            }
            return soNgay;
        }
  
    }
}
